package com.ayf.payment.game.api.sms;

import com.ayf.payment.game.api.entity.platform.SmsChannelConfig;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.platform.SmsChannelConfigService;
import com.ayf.payment.game.api.type.SMSType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description: 短信发送工具
 * @Author: zh-liang
 * @Date : 2020-04-12 16:30
 */
@Component
public class SMSUtil {

    @Autowired
    private SmsChannelConfigService smsChannelConfigService;

    @Autowired
    private GetSMSHandler getSMSHandler;

    public void send(String phone,SMSType type,Integer merchantId) {
        if (type == null) {
            throw new ParameterException("短信类型不能为空");
        }
        SMSHandler handler = getSMSHandler.get(getSMSConfig().getChannelCode());
        handler.send(phone,type, getSMSConfig(), merchantId);
    }

    public void send(String phone, SMSType type) {
        send(phone, type, null);
    }

    private SmsChannelConfig getSMSConfig() {
        return smsChannelConfigService.selectSmsConfig();
    }
}
